package io.github.common.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.common.entity.req.AbstractReq;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 
 *
 * @author haijun
 * @version 1.0.0
 * @email "mailto:haijun@qq.com"
 * @date 2024.11.10 20:44
 * @since 1.0.0
 */
@Slf4j
@UtilityClass
public class Condition {

    /**
     * SQL_REGEX
     */
    private static final String SQL_REGEX =
            "'|%|--|insert|delete|update|select|count|group|union|drop|truncate|alter|grant|execute|exec|xp_cmdshell|call|declare|sql";

    /**
     * EQUAL
     */
    private static final String EQUAL = "_equal";
    /**
     * NOT_EQUAL
     */
    private static final String NOT_EQUAL = "_notequal";
    /**
     * LIKE
     */
    private static final String LIKE = "_like";
    /**
     * NOT_LIKE
     */
    private static final String NOT_LIKE = "_notlike";
    /**
     * GT
     */
    private static final String GT = "_gt";
    /**
     * LT
     */
    private static final String LT = "_lt";
    /**
     * DATE_GT
     */
    private static final String DATE_GT = "_dategt";
    /**
     * DATE_EQUAL
     */
    private static final String DATE_EQUAL = "_dateequal";
    /**
     * DATE_LT
     */
    private static final String DATE_LT = "_datelt";
    /**
     * IS_NULL
     */
    private static final String IS_NULL = "_null";
    /**
     * NOT_NULL
     */
    private static final String NOT_NULL = "_notnull";
    /**
     * IGNORE
     */
    private static final String IGNORE = "_ignore";

    /**
     * 转化成mybatis plus中的Page
     *
     * @param <T>   the type parameter
     * @param query the query
     * @return the page
     * @since 2024.1.1
     */
    public static <T> IPage<T> getPage(AbstractReq query) {
        return new Page<>(query.getPage(), query.getSize());
    }

    /**
     * 获取 mybatis plus 中的 QueryWrapper
     *
     * @param <T>    the type parameter
     * @param entity the entity
     * @return query wrapper
     * @since 2024.1.1
     */
    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    /**
     * 获取 mybatis plus中的 QueryWrapper
     *
     * @param <T>   the type parameter
     * @param query the query
     * @param clazz the clazz
     * @return query wrapper
     * @since 2024.1.1
     */
    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
        query.remove("page" );
        query.remove("limit" );
        query.remove("descs" );
        query.remove("ascs" );
        QueryWrapper<T> qw = new QueryWrapper<>();
        return qw;
    }

}